package org.gnomus.data;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Transaction;

import java.util.HashMap;
import java.util.Map;

public class object extends HashMap<String, Object> {
    public String kind = null;
    public Key key = null;

    public object() {
    }

    public object(Entity e) {
        kind = e.getKind();
        key = e.getKey();
        setId((String) e.getProperty("id"));
        setCreated((Long) e.getProperty("created"));
    }

    public object(String kind) {
        this.kind = kind;
    }

    public void save() {
        assert kind != null;
        if (key != null) {
            Entity e = store.get(key);
            for (Map.Entry<String, Object> me : entrySet()) {
                e.setProperty(me.getKey(), me.getValue());
            }
            store.put(e);
        } else {
            key = store.put(kind, this);
            Entity e = store.get(key);
            setId((String) e.getProperty("id"));
            setCreated((Long) e.getProperty("created"));
        }
    }

    public void setId(String id) {
        put("id", id);
    }

    public String getId() {
        return (String) get("id");
    }

    public void setCreated(Long created) {
        put("created", created);
    }

    public Long getCreated() {
        return (Long) get("created");
    }
}
